package org.particles.gui;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;

/**
 * run/stop simulation.
 */
public class RunSimulationAction extends AbstractSimAction {
	private static final long serialVersionUID = -4108846759943381439L;
	private static final String playSmallIconName = "Play16";
	private static final String playBigIconName = "Play24";
	private static final String stopSmallIconName = "Stop16";
	private static final String stopBigIconName = "Stop24";

	public RunSimulationAction() {
		super();
	}

	/**
	 * must be called after all properties (task, commonService) are set.
	 */
	public void initAction() {
		putValue(SHORT_DESCRIPTION, "Run/Stop simulation");
		putValue(MNEMONIC_KEY, KeyEvent.VK_G);
		setPlayIcon();
		putValue(NAME, "Go");
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		getWorldTimeTask().setRunning(!getWorldTimeTask().isRunning());
		if(getWorldTimeTask().isRunning()) {
			setStopIcon();
		}
		else {
			setPlayIcon();
		}
	}
	
	public void setPlayIcon() {
		putValue(SMALL_ICON, new ImageIcon(getCommonService().findImageUrlByName(playSmallIconName)));
		putValue(LARGE_ICON_KEY, new ImageIcon(getCommonService().findImageUrlByName(playBigIconName)));
	}
	
	public void setStopIcon() {
		putValue(SMALL_ICON, new ImageIcon(getCommonService().findImageUrlByName(stopSmallIconName)));
		putValue(LARGE_ICON_KEY, new ImageIcon(getCommonService().findImageUrlByName(stopBigIconName)));
	}

}
